import 'package:flutter/material.dart';

class NavigatorTestWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: "Navigator Test",
      theme: new ThemeData(primarySwatch: Colors.blue),
      home: new NavigatorTestPage(
        label: "home",
        key_nav: '/',
      ),
      routes: <String, WidgetBuilder>{
        '/a': (BuildContext context) =>
            new NavigatorTestPage(label: 'page A', key_nav: '/b'),
        '/b': (BuildContext context) =>
            new NavigatorTestPage(label: 'page B', key_nav: '/c'),
        '/c': (BuildContext context) =>
            new NavigatorTestPage(label: 'page C', key_nav: '/'),
      },
    );

    /*return new MaterialApp(
      title: 'Sample ListView',
      theme: new ThemeData(primarySwatch: Colors.blue),
      home: new TempTestPage(
        label: "home",
      ),
      routes: <String, WidgetBuilder>{
        '/a': (BuildContext context) => new TempTestPage(
              label: "Page A",
            )
      },
    );*/
  }
}

class TempTestPage extends StatefulWidget {
  String label;

  TempTestPage({Key key, this.label}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return new TempTestState();
  }
}

class TempTestState extends State<TempTestPage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Center(
        child: new Container(
            child: new GestureDetector(
          child: new Text(widget.label),
          onTap: () {
            onClick("哈哈");
          },
        )),
      ),
    );
  }

  void onClick(String content) {
    print("谁点我 ${content}");
    Navigator.of(context).pushNamed('/a');
  }
}

class NavigatorTestPage extends StatefulWidget implements OnClickListener {
  String label;
  String key_nav;
  OnClickListener onClickListener;

  NavigatorTestPage({Key key, this.label, this.key_nav}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return new _NavigatorTestState();
  }

  @override
  onClick(BuildContext context, String label) {
    print("打印 ${label}");
    Navigator.of(context).pushNamed(label);
  }
}

class _NavigatorTestState extends State<NavigatorTestPage> {

  _NavigatorTestState();

  @override
  Widget build(BuildContext context) {
    var tx = new Text(widget.label);
    return new Scaffold(
      body: new Center(
        child: new Container(
          alignment: Alignment.center,
          padding: EdgeInsets.all(10.0),
          child: GestureDetector(
            child: tx,
            // onTap: this.onClickListener?.onClick(context, widget.key_nav),
          ),
        ),
      ),
    );
  }
}

abstract class OnClickListener {
  onClick(BuildContext context, String key);
}
